package cn.le.utils;

import cn.hutool.core.util.ReflectUtil;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author le
 * @Date 2020/2/26 17:09
 * @Version 0.1
 **/

public class SqlUtils {

    public static String getSql(AnalysisSql dto){
        Class clas = dto.getClass();
        Field[] fields = ReflectUtil.getFields(clas);
        //利用字段获取值
        String order = "";
        StringBuilder sb = new StringBuilder();
        for (Field f : fields) {
            String name = f.getName();
            if ("pageNum".equals(name) || "pageSize".equals(name)){
                continue;
            }
            if ("order".equals("")){
                order = ReflectUtil.invoke(dto,"getOrder");
                continue;
            }
            char[] chars = name.toCharArray();
            if (chars[0] >= 'a' && chars[0] <= 'z') {
                chars[0] = (char)(chars[0] - 32);
            }
            String method = "get" + new String(chars);
            String val = ReflectUtil.invoke(dto,method);
            sb.append(val);
        }
        return sb.append(order).toString();
    }

    /**
     * 等价表达式
     * @param column
     * @param value
     * @return
     */
    public static String equivalent(String column,String value){
        if (StringUtils.isEmpty(value)){
            return "";
        }
        List<String> params = analysis(value);
        StringBuilder sb = new StringBuilder();
        sb.append(" and ");
        for (int i = 0,size = params.size(); i < size; i++){
            sb.append(column).append(params.get(i));
            if (i < (size - 1)){
                sb.append(" and ");
            }
        }
        return sb.toString();
    }

    /**
     * like 操作
     * @param column
     * @param value
     * @return
     */
    public static String like(String column,String value){
        if (StringUtils.isEmpty(value)){
            return "";
        }
        List<String> params = analysis(value);
        StringBuilder sb = new StringBuilder();
        sb.append(" and (");
        for (int i = 0,size = params.size(); i < size; i++){
            sb.append(column).append(" like '%").append(params.get(i)).append("%'");
            if (i < (size - 1)){
                sb.append(" or ");
            }
        }
        sb.append(")");
        return sb.toString();
    }

    public static String notLike(String column,String value){
        if (StringUtils.isEmpty(value)){
            return "";
        }
        List<String> params = analysis(value);
        StringBuilder sb = new StringBuilder();
        sb.append(" and (");
        for (int i = 0,size = params.size(); i < size; i++){
            sb.append(column).append(" not like '%").append(params.get(i)).append("%'");
            if (i < (size - 1)){
                sb.append(" and ");
            }
        }
        sb.append(")");
        return sb.toString();
    }


    /**
     * 返回多种数据
     * @param many
     * @return
     */
    public static List<String> analysis(String many){
        many = many.replace("，",",");
        List<String> list = new ArrayList<>();
        String[] param = many.split(",");
        for (int i = 0; i < param.length; i++) {
            list.add(param[i]);
        }
        return list;
    }
}
